package com.yanqu.road.dao.impl.zhaoshang;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.zhaoshang.ZsUserCoolDown;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ZsUserCoolDownDaoImpl extends TempDataObjectDao<ZsUserCoolDown> {


    public static String getZsUserCoolDownSql = "select * from t_c_zs_cool_down ";

    public Map<Long, ZsUserCoolDown> getZsUserCoolDownMap() {
        Map<Long, ZsUserCoolDown> dataMap = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getZsUserCoolDownSql);
            rs = call.executeQuery();
            while (rs.next()) {
                ZsUserCoolDown zsUserCoolDown = parse(rs);
                dataMap.put(zsUserCoolDown.getUserId(), zsUserCoolDown);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public static String updateZsUserCoolDownSql = "update t_c_zs_cool_down set serverId=?,lastUpdateTime=? where userId=?";
    @Override
    protected boolean updateData(ZsUserCoolDown zsUserCoolDown) {
        zsUserCoolDown.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, zsUserCoolDown.getServerId()));
        para.put(i++, new DbParameter(Types.BIGINT, zsUserCoolDown.getLastUpdateTime()));
        para.put(i++, new DbParameter(Types.BIGINT, zsUserCoolDown.getUserId()));
        if (getDb().executeNoneQuery(updateZsUserCoolDownSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertZsUserCoolDownSql = "insert into t_c_zs_cool_down(userId,serverId,lastUpdateTime) values (?,?,?)";
    @Override
    protected boolean addData(ZsUserCoolDown zsUserCoolDown) {
        zsUserCoolDown.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, zsUserCoolDown.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, zsUserCoolDown.getServerId()));
        para.put(i++, new DbParameter(Types.BIGINT, zsUserCoolDown.getLastUpdateTime()));
        if (getDb().executeNoneQuery(insertZsUserCoolDownSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected ZsUserCoolDown parseData(ResultSet rs) throws SQLException {
        ZsUserCoolDown zsUserCoolDown = new ZsUserCoolDown();
        zsUserCoolDown.setUserId(rs.getLong("userId"));
        zsUserCoolDown.setServerId(rs.getLong("serverId"));
        zsUserCoolDown.setLastUpdateTime(rs.getLong("lastUpdateTime"));
        return zsUserCoolDown;
    }
}
